<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */


namespace App\Http\Admin\Company\Controllers;

use App\Eloquent\Area;
use App\Eloquent\City;
use App\Eloquent\Province;
use App\Eloquent\Ygt\Address;
use App\Eloquent\Ygt\Customer;
use App\Eloquent\Ygt\DepartmentUser;
use Framework\BaseClass\Http\Admin\Controller;

class AddressController extends Controller
{
    public function index()
    {
    }
    public function lists()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];
        $customerId         = request('customer_id',0);
        if (request()->isMethod('post')) {

            $pageSize       = request('pageSize', 30);
            $pageCurrent    = request('pageCurrent', 1);
            $offset         = ($pageCurrent - 1) * $pageSize;

            $where          = ['company_id'=>$companyId];
            if($customerId>0)
            {
                $where['foreign_key']   = $customerId;
            }
            $lists          = Address::getList($where,'',$pageSize,$offset)->toArray();
            $count          = Address::getCount($where);
            $data           = ['totalRow'=>$count,'pageCurrent'=>$pageCurrent,'list'=>$lists];
            $result         = json_encode($data);
            return $result;
        } else {
            return $this->view('lists',compact('customerId'));
        }
    }

    public function edit()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $data               = request()->all();
        $isSubmit           = isset($data['is_submit']) ? $data['is_submit'] : 0;
        $id                 = request('id',0);
        if($isSubmit){
            unset($data['is_submit']);
            $data['company_id']     = $companyId;
            $result                   = Address::putInData($data);
            if( $result ){
                return $this->ajaxSuccess( '成功' );
            } else {
                return $this->ajaxFail( '失败' );
            }
        } else {
            $where                  = ['company_id'=>$companyId];
            $foreignList            = Customer::getList($where);
            $provinceList           = Province::all();
            $info                   = $data;
            if($id){
                $where              = ['id'=>$id];
                $info               = Address::getInfo($where)->toArray();
            }
            return $this->view('edit',compact('foreignList','provinceList','info'));
        }
    }
    public function ajaxAreaList()
    {
        $type               = request('type',1);
        $value              = request('value',0);
        switch ($type)
        {
            case 1:
                $data       = Province::get()->toArray();
                break;
            case 2:
                $where      = ['fatherID'=>$value];
                $data       = City::where($where)->get()->toArray();
                break;
            case 3:
                $where      = ['fatherID'=>$value];
                $data       = Area::where($where)->get()->toArray();
                break;
            default:
                $data       =[];
        }
        $result             = json_encode($data);
        return $result;
    }
    public function delete()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $id                 = request('id');
        if( $id ){
            $idArr          = explode(',',$id);
            $where          = ['company_id'=>$companyId,'id'=>['in',$idArr]];
            $result         = Address::del($where);
            if($result){
                return $this->ajaxSuccess( '成功' );
            } else {
                return $this->ajaxFail('删除失败');
            }
        } else {
            return $this->ajaxFail('失败3');
        }
    }
}